Java8使用stream操作两个list根据某字段匹配再对其中一个list进行赋值

您所在的位置:网站首页 两个list stream Java8使用stream操作两个list根据某字段匹配再对其中一个list进行赋值

Java8使用stream操作两个list根据某字段匹配再对其中一个list进行赋值

2023-05-10 22:44| 来源: 网络整理| 查看: 265

Java8使用stream操作两个list根据某字段匹配再对其中一个list进行赋值 原创

公众号JavaEdge 2022-01-10 14:09:50 ©著作权

文章标签 数据结构 算法 java 原力计划 取对象 文章分类 代码人生

©著作权归作者所有:来自51CTO博客作者公众号JavaEdge的原创作品,请联系作者获取转载授权,否则将追究法律责任

import com.google.common.collect.Lists;import lombok.extern.slf4j.Slf4j;import java.lang.reflect.Field;import java.util.*;import java.util.stream.Collectors;@Slf4jpublic class ListUtils { /** * lambda表达式对两个List进行循环,根据符合条件,进行相关的赋值操作并返回这个对象集合 * @param sourceList 待设置源列表 * @param srcEqualProp 源对象条件判断属性名 * @param srcSetProp 源对象待设置属性名 * @param targetList 资源提供者列表 * @param tarEqualProp 对象条件判断参数名 * @param tarGetProp 待获取对象属性名 * @param * @param * @return */ public static List setListByEqualObjProperty(List sourceList, String srcEqualProp, String srcSetProp, List targetList, String tarEqualProp, String tarGetProp){ List resultList = Lists.newArrayList(); resultList = sourceList.stream() .map(sur-> targetList.stream() .filter(tar -> Objects.equals(getValueByPropName(sur, srcEqualProp), getValueByPropName(tar, tarEqualProp))) .findFirst() .map(tar -> { setValueByPropName(sur, srcSetProp, getValueByPropName(tar, tarGetProp)); return sur; } ).orElse(null)) .collect(Collectors.toList()); return resultList; } /** * 通过遍历两个List中按id属性相等的归结到resultList中 * @param oneList 源list 1 * @param twoList 源list 2 * @param equalKeyName 相等的map键值 */ public static List compareListHitData(List oneList, List twoList, Object equalKeyName) { List resultList = oneList.stream().map(map -> twoList.stream() .filter(m -> Objects.equals(m.get(equalKeyName),map.get(equalKeyName))) .findFirst().map(m -> { map.putAll(m); return map; }).orElse(null)) .filter(Objects::nonNull).collect(Collectors.toList()); return resultList; } // 通过属性获取传入对象的指定属性的值 public static T getValueByPropName(Object object, String propName) { T value = null; try { // 通过属性获取对象的属性 Field field = object.getClass().getDeclaredField(propName); // 对象的属性的访问权限设置为可访问 field.setAccessible(true); // 获取属性的对应的值 value = (T)field.get(object); } catch (Exception e) { return null; } return value; } // 通过属性设置传入对象的指定属性的值 public static void setValueByPropName(Object object, String propName, U updateValue) { try { // 通过属性获取对象的属性 Field field = object.getClass().getDeclaredField(propName); // 对象的属性的访问权限设置为可访问 field.setAccessible(true); // 设置属性的对应的值 field.set(object, updateValue); } catch (Exception e) { log.error("setValueByPropName.error {}", propName, e); } } @Datapublic class Girl { private String id; private String name;}@Datapublic class SchoolBoy { private String girlId; private String id; private String name; private Integer age; private String girlName;} public static void main(String[] args) { List schoolBoys = new ArrayList(3); SchoolBoy boy1 = new SchoolBoy(); boy1.setGirlId("1"); boy1.setId("10"); boy1.setName("小明"); SchoolBoy boy2 = new SchoolBoy(); boy2.setGirlId("2"); boy2.setId("11"); boy2.setName("小豪"); SchoolBoy boy3 = new SchoolBoy(); boy3.setGirlId("3"); boy3.setId("12"); boy3.setName("小白"); schoolBoys.add(boy1); schoolBoys.add(boy2); schoolBoys.add(boy3); List girls = new ArrayList(3); Girl girl1 = new Girl(); girl1.setId("1"); girl1.setName("小英"); Girl girl2 = new Girl(); girl2.setId("2"); girl2.setName("小美"); Girl girl3 = new Girl(); girl3.setId("3"); girl3.setName("小花"); girls.add(girl1); girls.add(girl2); girls.add(girl3); List list = ListUtils.setListByEqualObjProperty(schoolBoys,"girlId", "girlName", girls, "id", "name"); System.out.println(list.toString()); List oneList = new ArrayList(); Map oneMap = new HashMap(); oneMap.put("id", 111); oneMap.put("userName", "林飞"); Map twoMap = new HashMap(); twoMap.put("id", 222); twoMap.put("userName", "Hejinrong"); oneList.add(oneMap); oneList.add(twoMap); List twoList = new ArrayList(); Map threeMap = new HashMap(); threeMap.put("id", 111); threeMap.put("userName", "林飞"); Map fourMap = new HashMap(); fourMap.put("id", 333); fourMap.put("userName", "Hejinrong"); twoList.add(threeMap); twoList.add(fourMap); List resultList = compareListHitData(oneList, twoList, "id"); System.out.println(resultList); System.out.println("Max memory =" + Runtime.getRuntime().maxMemory()/(double)1024/1024 +"M"); System.out.println("Total memory= " + Runtime.getRuntime().totalMemory()/(double)1024/1024 +"M"); }}

打赏 收藏 评论 分享 举报

上一篇:MySQL的int (10) 和 int (11) 的区别

下一篇:为什么会有人写 where1=1?



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3